/*
 * PZonas.java
 *
 * Created on 18 de febrero de 2007, 07:45 PM
 */

package ig.metrica.ui.matrizcorrelacion;

import ig.metrica.riesgo.AjustesAlternosDTO;
import ig.metrica.riesgo.ZonaDTO;
import ig.util.MyTableModel;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Enumeration;
import java.util.Hashtable;

/**
 *
 * @author  jsanchez
 */
public class PZonas extends javax.swing.JPanel implements IPanel {
    public Hashtable<IPanel,Integer> oyentes=new Hashtable<IPanel,Integer>();
    /** Creates new form PZonas */
    public PZonas() {
        initComponents();
        this.TAjustesAlter.getTableHeader().setReorderingAllowed(false);
        this.TAjustesAlter.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);

        this.TZonas.getTableHeader().setReorderingAllowed(false);
        this.TZonas.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jSplitPane1 = new javax.swing.JSplitPane();
        jScrollPane1 = new javax.swing.JScrollPane();
        TZonas = new javax.swing.JTable();
        jScrollPane2 = new javax.swing.JScrollPane();
        TAjustesAlter = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();

        jSplitPane1.setDividerLocation(260);

        TZonas.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        TZonas.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                TZonasMouseReleased(evt);
            }
        });
        jScrollPane1.setViewportView(TZonas);

        jSplitPane1.setLeftComponent(jScrollPane1);

        TAjustesAlter.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        jScrollPane2.setViewportView(TAjustesAlter);

        jSplitPane1.setRightComponent(jScrollPane2);

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText(org.openide.util.NbBundle.getMessage(PZonas.class, "PZonas.jLabel1.text")); // NOI18N

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jSplitPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 553, Short.MAX_VALUE)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .add(jLabel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 531, Short.MAX_VALUE)
                .add(10, 10, 10))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(jLabel1)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jSplitPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 301, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents
    
    private void TZonasMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_TZonasMouseReleased
        if(htaa!=null){
            if(this.TZonas.getSelectedRow()>-1){
                ZonaDTO zona=(ZonaDTO)((MyTableModel)this.TZonas.getModel()).getValueAt(this.TZonas.getSelectedRow(),0);
                
                int i=0;
                for (AjustesAlternosDTO elem : htaa) {
                    if(zona.getId().equals(elem.getIdZona1()) || zona.getId().equals(elem.getIdZona2())){
                        i++;
                    }
                }
                Object[][] data=new Object[i][2];
                i=0;                
                for (AjustesAlternosDTO elem : htaa) {
                    if(zona.getId().equals(elem.getIdZona1()) || zona.getId().equals(elem.getIdZona2())){
                        data[i++][0]=elem.getIdZona1().equals(zona.getId())?elem.getIdZona2():elem.getIdZona1();
                        data[i-1][1]=elem.getAjuste();
                    }
                }
                
                this.TAjustesAlter.setModel(new MyTableModel(data,new String [] {"Zona Horizontal","Ajuste Horizontal entre zonas"},new boolean [] {true,true}));
            }
        }
    }//GEN-LAST:event_TZonasMouseReleased
    Hashtable ht=null;
    Collection<AjustesAlternosDTO> htaa=new ArrayList<AjustesAlternosDTO>();
    //@Override
    public void update(Object obj) {
        System.out.println("soy zona");
        if(obj instanceof Hashtable){
            
//            htable.put("TIPO",panel.getTipo());
//            htable.put("ZONAS",panel.getZonas());
//            htable.put("AJUSTES_ALTERNOS",panel.getAjustesAlternos());
//            htable.put("DTO",panel.getDTO());
            
            ht=(Hashtable) obj;
            if(ht.get("ZONAS")!=null){
                Object[][] data=new Object[((Collection<ZonaDTO>)ht.get("ZONAS")).size()][2];
                int i=0;
                for (ZonaDTO elem : (Collection<ZonaDTO>)ht.get("ZONAS")) {
                    data[i++][0]=elem;
                    data[i-1][1]=elem.getAjusteHorizontal();
                }
                this.TZonas.setModel(new MyTableModel(data,new String [] {"Nombre","Ajuste Horizontal"},new boolean [] {true,true}));
            }
            if(ht.get("AJUSTES_ALTERNOS")!=null){
                htaa=(Collection)ht.get("AJUSTES_ALTERNOS");
            }
            
        }else if(obj instanceof Collection){
            System.out.println("Aculizar las bandas");
        }
    }
    
    
    //@Override
    public String getTitle() {
        return this.getName();
    }
    
    //@Override
    public void setTitle(String title) {
        this.setName(title);
    }
    
    //@Override
    public void addOyente(IPanel oyente, Integer integer) {
        oyentes.put(oyente,integer);
    }
    
    //@Override
    public void notificarSiguiente() throws Exception{
        System.out.println("Notifica Zona");
        Enumeration<IPanel> enume=oyentes.keys();
        while(enume.hasMoreElements()){
            IPanel ipanel=enume.nextElement();
            if(oyentes.get(ipanel).equals(0)){
                if(this.TZonas.getSelectedRow()>-1){
                    ZonaDTO zona=(ZonaDTO)((MyTableModel)this.TZonas.getModel()).getValueAt(this.TZonas.getSelectedRow(),0);
                    ipanel.update(zona.getBandas());
                }else
                    throw new Exception("Seleccione una zona.");
            }
        }
    }
    
    //@Override
    public void notificarAnterior() throws Exception{
        Enumeration<IPanel> enume=oyentes.keys();
        while(enume.hasMoreElements()){
            IPanel ipanel=enume.nextElement();
            if(oyentes.get(ipanel).equals(1))
                ipanel.update(ht);
        }
    }
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTable TAjustesAlter;
    private javax.swing.JTable TZonas;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JSplitPane jSplitPane1;
    // End of variables declaration//GEN-END:variables

    public Collection getData() {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    
}
